package scau.erps.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import scau.erps.app.Erps;
import scau.erps.entity.Sand;
import scau.erps.entity.Store;
import scau.erps.view.decision.sand.FactoryPanel;
import scau.erps.view.decision.sand.HighLoanPanel;
import scau.erps.view.decision.sand.IsoPanel;
import scau.erps.view.decision.sand.LongLoanPanel;
import scau.erps.view.decision.sand.MarketPanel;
import scau.erps.view.decision.sand.PayablePanel;
import scau.erps.view.decision.sand.ProductPanel;
import scau.erps.view.decision.sand.ReceivablePanel;
import scau.erps.view.decision.sand.ShortLoanPanel;

/**
 * 在主窗口用Swing模拟沙盘界面
 * 使用了练习模式decision包中的相关界面类，统一教学模式和练习模式界面。
 * @author DCJ
 *
 */
public class SandPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private ShortLoanPanel shortLoan;
	private LongLoanPanel longLoan;
	private HighLoanPanel highLoan;
	private PayablePanel payable;
	private ReceivablePanel receivable;
	private ProductPanel crystal,ruby,sapphire;
	private MarketPanel district,domestic,asia,internation;
	private IsoPanel iso9000,iso14000;
	private JTable costTable,storeTable,orderTable;
	public SandPanel(){
		JPanel sandPanel = new JPanel();
		sandPanel.setLayout(new BorderLayout(5,5));	
		sandPanel.add(getCostPanel(),BorderLayout.NORTH);
		sandPanel.add(getCenterPanel(),BorderLayout.CENTER);
		sandPanel.add(getOrderPanel(),BorderLayout.SOUTH);
		sandPanel.add(getResearchPanel(),BorderLayout.EAST);		
		this.add(sandPanel);
	}
	private JScrollPane getCostPanel(){			
		costTable = new JTable(getCostTableModel());
		costTable.setEnabled(false);
		costTable.setRowHeight(40);
		costTable.setPreferredScrollableViewportSize(new Dimension(820, 40));
		JScrollPane scrollPane = new JScrollPane(costTable);
		scrollPane.setBorder(BorderFactory.createTitledBorder("综合费用"));
		scrollPane.setViewportView(costTable);
		return scrollPane;		
	}
	private CostTableModel getCostTableModel(){
		CostTableModel costModel = new CostTableModel();
		return costModel;		
	}

	private JPanel getFinancePanel(){
		JPanel financePanel = new JPanel();
		financePanel.setBorder(BorderFactory.createTitledBorder("财务中心"));
		financePanel.setLayout(new GridLayout(1,6));
		shortLoan = new ShortLoanPanel();
		longLoan = new LongLoanPanel();
		highLoan = new HighLoanPanel();
		payable = new PayablePanel();
		receivable = new ReceivablePanel();
		financePanel.add(shortLoan);
		financePanel.add(longLoan);
		financePanel.add(highLoan);
		financePanel.add(payable);	
		financePanel.add(receivable);
		return financePanel;
	}
	
	private JScrollPane getStorePanel(){		
		
		storeTable = new JTable(getStoreTableModel());
		storeTable.setEnabled(false);
		storeTable.setRowHeight(40);
		storeTable.setPreferredScrollableViewportSize(new Dimension(530, 40));
		JScrollPane scrollPane = new JScrollPane(storeTable);
		scrollPane.setBorder(BorderFactory.createTitledBorder("供应中心"));
		scrollPane.setViewportView(storeTable);
		return scrollPane;		
	}	
	private StoreTableModel getStoreTableModel(){
		StoreTableModel storeModel = new StoreTableModel();
		return storeModel;
	}

	private JPanel getFactoryPanel(){
		JPanel factoryPanel = new JPanel(new GridLayout(1,8));
		factoryPanel.setBorder(BorderFactory.createTitledBorder("生产中心"));
		FactoryPanel factory = new FactoryPanel();
		factory.setDimension(new Dimension(520,200));
		factoryPanel.add(factory);
		return factoryPanel;		
	}
	private JPanel getCenterPanel(){
		JPanel centerPanel = new JPanel(new BorderLayout());
		centerPanel.add(getFinancePanel(),BorderLayout.NORTH);			
		centerPanel.add(getStorePanel(),BorderLayout.CENTER);
		centerPanel.add(getFactoryPanel(),BorderLayout.SOUTH);
		return centerPanel;		
	}
	private JScrollPane getOrderPanel(){		
		orderTable = new JTable(getOrderTableModel());
		orderTable.setEnabled(false);
		orderTable.setRowHeight(40);
		orderTable.setPreferredScrollableViewportSize(new Dimension(820, 40));
		JScrollPane scrollPane = new JScrollPane(orderTable);
		scrollPane.setBorder(BorderFactory.createTitledBorder("销售中心"));
		scrollPane.setViewportView(orderTable);
		return scrollPane;		
	}
	private OrderTableModel getOrderTableModel(){
		OrderTableModel orderModel = new OrderTableModel();
		return orderModel;
	}

	private JPanel getResearchPanel(){
		JPanel researchPanel = new JPanel(new BorderLayout());
		researchPanel.setBorder(BorderFactory.createTitledBorder("研发中心"));		
		JPanel productPanel= new JPanel();
		productPanel.setPreferredSize(new Dimension(270,225));
		productPanel.setBorder(BorderFactory.createTitledBorder("产品研发"));
		crystal = new ProductPanel("Crystal");
		ruby = new ProductPanel("Ruby");
		sapphire = new ProductPanel("Sapphire");
		productPanel.add(crystal);
		productPanel.add(ruby);
		productPanel.add(sapphire);
		JPanel marketPanel= new JPanel();	
		marketPanel.setBorder(BorderFactory.createTitledBorder("市场开拓"));
		marketPanel.setPreferredSize(new Dimension(270,150));
		district = new MarketPanel("区域");
		domestic = new MarketPanel("国内");
		asia = new MarketPanel("亚洲");
		internation = new MarketPanel("国际");
		marketPanel.add(district);
		marketPanel.add(domestic);
		marketPanel.add(asia);
		marketPanel.add(internation);
		JPanel isoPanel= new JPanel();
		isoPanel.setBorder(BorderFactory.createTitledBorder("ISO认证"));
		isoPanel.setPreferredSize(new Dimension(270,115));
		iso9000 = new IsoPanel("ISO9000");
		iso14000 = new IsoPanel("ISO14000");
		isoPanel.add(iso9000);
		isoPanel.add(iso14000);				
		researchPanel.add(productPanel,BorderLayout.NORTH);
		researchPanel.add(marketPanel,BorderLayout.CENTER);
		researchPanel.add(isoPanel,BorderLayout.SOUTH);
		return researchPanel;		
	}
	public void setDashBoardData(){
		this.costTable.setModel(this.getCostTableModel());
		this.storeTable.setModel(this.getStoreTableModel());
		this.orderTable.setModel(this.getOrderTableModel());
		this.shortLoan.setTextFieldValue();
		this.longLoan.setTextFieldValue();
		this.highLoan.setTextFieldValue();
		this.payable.setTextFieldValue();
		this.receivable.setTextFieldValue();
		this.crystal.setCheckBoxValue("Crystal");
		this.ruby.setCheckBoxValue("Ruby");
		this.sapphire.setCheckBoxValue("Sapphire");
		this.district.setCheckBoxValue("区域");
		this.domestic.setCheckBoxValue("国内");
		this.asia.setCheckBoxValue("亚洲");
		this.internation.setCheckBoxValue("国际");
		this.iso9000.setCheckBoxValue("ISO9000");
		this.iso14000.setCheckBoxValue("ISO14000");
	}
}
class CostTableModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	String[] title = {"现金","所得税","贴息", "利息","维修费","转产费","折旧","广告费","租金","其他","管理费"};	
	Object[][] cost = null;
	public CostTableModel(){
		cost = new Object[1][11];
		Sand sand = Erps.getSand();
		cost[0][0] = sand.getCash();
		cost[0][1] = sand.getTax();
		cost[0][2] = sand.getDiscount();
		cost[0][3] = sand.getInterest();
		cost[0][4] = sand.getMaintenanceFee();
		cost[0][5] = sand.getSwitchFee();
		cost[0][6] = sand.getDepreciation();
		cost[0][7] = sand.getAdvertisingFee();
		cost[0][8] = sand.getRent();
		cost[0][9] = sand.getOther();	
		cost[0][10] = sand.getManagementFee();
	}			
	@Override
	public int getRowCount() {
		return this.cost.length; //行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; //列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return  cost[rowIndex][columnIndex];  //取表格单元格值
	}
	public String getColumnName(int columnIndex) {
        return title[columnIndex];  //显示标题
    }	
}
class StoreTableModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	String[] title = {"M1订单","M2订单","M3订单", "M4订单","M1仓库","M2仓库","M3仓库","M4仓库"};	
	Object[][] stores = null;
	public StoreTableModel(){
		stores = new Object[1][8];
		Store store = Erps.getStore();		
		stores[0][0] = new Integer(0);
		stores[0][1] = new Integer(0);
		stores[0][2] = new Integer(0);
		stores[0][3] = new Integer(0);
		stores[0][4] = store.getM1();
		stores[0][5] = store.getM2();
		stores[0][6] = store.getM3();
		stores[0][7] = store.getM4();	
	}			
	@Override
	public int getRowCount() {
		return this.stores.length; //行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; //列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return  stores[rowIndex][columnIndex];  //取表格单元格值
	}
	public String getColumnName(int columnIndex) {
        return title[columnIndex];  //显示标题
    }	
}
class OrderTableModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	String[] title = {"Beryl订单","Crystal订单","Ruby订单", "Sapphire订单","Beryl仓库","Crystal仓库","Ruby仓库","Sapphire仓库"};
	Object[][] orders = null;
	public OrderTableModel(){
		orders = new Object[1][8];			
		orders[0][0] = new Integer(0);
		orders[0][1] = new Integer(0);
		orders[0][2] = new Integer(0);
		orders[0][3] = new Integer(0);
		orders[0][4] = new Integer(0);
		orders[0][5] = new Integer(0);
		orders[0][6] = new Integer(0);
		orders[0][7] = new Integer(0);
	}			
	@Override
	public int getRowCount() {
		return this.orders.length; //行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; //列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return  orders[rowIndex][columnIndex];  //取表格单元格值
	}
	public String getColumnName(int columnIndex) {
        return title[columnIndex];  //显示标题
    }	
}
